<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/21
 * Time: 9:17
 */

namespace App\GraphQL\Query;

use App\Model\Job;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Query;

class JobsQuery extends Query
{



    protected $attributes = [
        'name' => 'jobs'
    ];

    public function type(): Type
    {
        return Type::listOf(\GraphQL::type('jobsType'));
    }

    /**
     * 接收参数的类型定义
     * @return array
     */
    public function args() :array
    {
        return [
            'id' => ['name' => 'id', 'type' => Type::int()],
            'name' => ['name' => 'name', 'type' => Type::string()],
            'user_id' => ['name' => 'user_id', 'type' => Type::int()],
        ];
    }

    /**
     * @param $root
     * @param $args 传入参数
     *
     * 处理请求的逻辑
     * @return mixed
     */
    public function resolve($root, $args)
    {
        //测试代码  users 就是本类 $attributes的name
        $qry = <<<TEST

        {
          jobss{
            id
            name
            user_id
          }
        }  

TEST;


       $qry = Job::query();

        if(isset($args['id']))
        {
            $qry->where('id' , $args['id']);
        }

        if(isset($args['name']))
        {
            $qry->where('name', $args['name']);
        }

        return $qry->get();
    }



}
